import { login } from "../js/interface.js"
import { setCookie, getCookie, deleteCookie } from "../js/cookie.js"

$(".albb .icon-QQ").mouseenter(function(){
    $(this).parents(".fa").css("backgroundColor","#506A7E");
    $(this).css("color","#fff");
}).mouseleave(function(){
    $(this).parents(".fa").css("backgroundColor","#f1f1f1");
    $(this).css("color","#191919");
})
$(".albb .icon-zhifubaozhifu").mouseenter(function(){
    $(this).parents(".fa").css("backgroundColor","blue");
    $(this).css("color","#fff");
}).mouseleave(function(){
    $(this).parents(".fa").css("backgroundColor","#f1f1f1");
    $(this).css("color","#191919");
})
$(".albb .icon-weixin").mouseenter(function(){
    $(this).parents(".fa").css("backgroundColor","green");
    $(this).css("color","#fff");
}).mouseleave(function(){
    $(this).parents(".fa").css("backgroundColor","#f1f1f1");
    $(this).css("color","#191919");
})




$("#account").blur(judgeAccount);
async function judgeAccount(){
    var account = $("#account").val().trim();
    $("#account").val(account);

    var reg = /^[a-zA-Z_]\w{5,11}$/;
    var reg2 = /^1[3-9]\d{9}$/;
    var reg3 = /^\w+@\w+\.(com|cn|edu)$/;

    if(reg.test(account) || reg2.test(account) || reg3.test(account)){
        $("#account-mes").html("√");
        $("#account-mes").addClass("right").removeClass("err");
        return true;
    }else{
        $("#account-mes").html("您输入的账号有误");
        $("#account-mes").addClass("err");
    }
    return false;
}

$("#pwd").blur(judgePwd);
async function judgePwd(){
    var pwd = $("#pwd").val().trim();
    $("#pwd").val(pwd);

    var reg = /^\w{6,12}$/;

    if (reg.test(pwd)) {
        $("#pwd-mes").html("密码已输入");
        $("#pwd-mes").addClass("right").removeClass("err");
        return true;
    } else {
        $("#pwd-mes").html("密码由数字、字母、下划线组成,6-12位");
        $("#pwd-mes").addClass("err");
    }
    return false;
}

$("#btn").click(async function(){
    var list = await Promise.all([judgeAccount(),judgePwd()]);
    var flag = list.every(item => item === true);
    var account = $("#account").val().trim();
    var pwd = $("#pwd").val().trim();
    
    if(account && pwd){
        login({ account, pwd }).then(result => {
            var { status, msg, user } = result;
            console.log(result);
            if (status) {
                // if (isRem.checked) {
                //     setCookie("lgc", user, 7);
                // } else {
                //     setCookie("lgc", user);
                // // }
                // location.href = "mall.html";
                
                setCookie("lgc", user);
                var url = urlParse();
                var { returnUrl } = url;
                if(returnUrl){
                    location.href = decodeURIComponent(returnUrl);
                }else{
                    location.href = "mall.html"
                }
            } else {
                // alert(msg);
                $("#pwd-mes").html(msg);
                $("#pwd-mes").addClass("err");
            }
        }).catch(err => {
            throw err;
        })
    }
})


var cookie = document.cookie;
if (cookie) {
    var lgc = getCookie("lgc");
    if (lgc) {
        $(".hide a:first-child").css("display", "none");
        $(".hide a:last-child").css("display", "block");
        $(".hide").eq(0).children().html(`欢迎,${lgc}`);
        // $(".hide a:last-child").html(`欢迎,${lgc}`);
        $(".exit").click(function () {
            deleteCookie("lgc");
            location.reload();
        })
    }
}



function urlParse() {
    var search = location.search;
    var data = {};
    if (search) {  //从注册页跳转过来  => 有数据
        var str = search.substring(1);
        console.log(str);
        var list = str.split("&");
        // console.log(list);
        list.forEach(function (item) {
            var key = item.split("=")[0];
            var val = item.split("=")[1];
            data[key] = val;
        })
    }
    return data;
}